﻿#include "qt_hread_process.h"




void callback(Mat image, void* p)
{
	
	
    QtThreadProcess* pCammerWidget = static_cast<QtThreadProcess*>(p);

	if (pCammerWidget->m_proess_state == true)
	{ 
	    //pCammerWidget->m_qDisplayFrameQueue.push_back(image);
	    //pCammerWidget->m_image_index++;
	
		int64 t1 = cv::getTickCount();
		emit pCammerWidget->sendCameraFinish(pCammerWidget->m_io_camera);

		
		VectorOutputResult result;
		result.m_image = image;
        result.m_detectin_result = pCammerWidget->m_function->detection(image, pCammerWidget->m_detection_times,result.m_messgae);
		result.m_camera_index = pCammerWidget->m_camera_index;
		result.m_detection_index = pCammerWidget->m_detection_index;
		result.m_detection_times = pCammerWidget->m_detection_times;
		result.m_camera_name = pCammerWidget->m_camera_name;
		result.m_image_draw = image;
		result.m_save_right = pCammerWidget->m_function->m_save_right;
		result.m_save_wrong = pCammerWidget->m_function->m_save_wrong;
		result.m_save_right_label = pCammerWidget->m_function->m_save_right_label;
		result.m_save_wrong_label = pCammerWidget->m_function->m_save_wrong_label;

		
	

		int64 t2 = cv::getTickCount();
	
		pCammerWidget->m_time_use = (t2 - t1) * 1000 / getTickFrequency();
		result.m_time_use = pCammerWidget->m_time_use;


			
        pCammerWidget->m_function->draw(image, result.m_image_draw);
        emit pCammerWidget->sendDetectionResult(result);

		
		pCammerWidget->m_detection_index++;
		emit pCammerWidget->sendComResult(pCammerWidget->m_io_result,0,pCammerWidget->m_io_finish);


	}
    if (pCammerWidget->m_only_show_state != false)
    {

        emit pCammerWidget->sendShowImage(image, pCammerWidget->m_camera_index, pCammerWidget->m_image_index);

    }
	pCammerWidget->m_image_index++;
	

}
QtThreadProcess::QtThreadProcess(QObject* parent)
{
    m_proess_state = false;
    m_only_show_state = false;

    qRegisterMetaType<Mat>("Mat");
    //对于自定义的结构体参数，信号槽无法识别参数，导致信号槽连接不起作用。
    //所以需要注册结构体参数。在结构体中声明结束的地方加上结构体注册。
    qRegisterMetaType<VectorOutputResult>("VectorOutputResult");

    

    m_image_index =0;
	
}

//void QtThreadProcess::run()
//{
//    while (1)
//    {
//        Mat image;
//        if (false == m_qDisplayFrameQueue.get(image))
//        {
//            continue;
//        }

//        if (m_proess_state == true)
//        {
//            //pCammerWidget->m_qDisplayFrameQueue.push_back(image);
//            //pCammerWidget->m_image_index++;

//            int64 t1 = cv::getTickCount();
//            emit sendCameraFinish(m_io_camera);


//            VectorOutputResult result;
//            result.m_image = image;
//            result.m_detectin_result = m_function->detection(image, m_detection_times,result.m_messgae);
//            result.m_camera_index =m_camera_index;
//            result.m_detection_index = m_detection_index;
//            result.m_detection_times = m_detection_times;
//            result.m_camera_name =m_camera_name;
//            result.m_image_draw = image;
//            result.m_save_right = m_function->m_save_right;
//            result.m_save_wrong = m_function->m_save_wrong;
//            result.m_save_right_label = m_function->m_save_right_label;
//            result.m_save_wrong_label = m_function->m_save_wrong_label;



//            //imwrite("d:/test.png", image);
//            int64 t2 = cv::getTickCount();

//            m_time_use = (t2 - t1) * 1000 / getTickFrequency();
//            result.m_time_use = m_time_use;



//           m_function->draw(image, result.m_image_draw);
//           emit sendDetectionResult(result);


//            m_detection_index++;
//            emit sendComResult(m_io_result,0,m_io_finish);


//        }
//        if (m_only_show_state != false)
//        {
//                emit sendShowImage(image,m_camera_index, m_image_index);

//        }
//        m_image_index++;
//    }
//}


